<?php
namespace app\mobile\controller;

use app\model\Util; 
use app\model\Fee; 
use app\model\Comm; 
use app\model\Theme; 
use app\model\Fans; 

class Mycomm extends Base
{
    public function Mycomm()
    {
global $_G, $_P;
$system=$_G['system'];
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('活动入口不正确');
}
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$p = array(":themeid"=>$themeid);
$pm = array(":themeid"=>$themeid,":openid"=>$_G['openid']);
$pu = array(":themeid"=>$themeid,":uid"=>$_G['member']['uid']);
if($op == 'display'){	
	$totalsuccess = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
	            WHERE m.themeid = :themeid and (status=1 or status=98 and type=99) and m.openid = :openid',$pm);
	if ($config['iscommshowfail'] == 1 || $system['autored']==2){
		if ($config['comm4autored']==3){
			$totalfail = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
			            WHERE m.themeid = :themeid and status=0 and type<>99 and m.openid = :openid',$pm);
		}else{
			$totalfail = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
			            WHERE m.themeid = :themeid and status=0 and type<>4 and type<>7 and type<>99 and m.openid = :openid',$pm);
		}
	}else{
		$totalfail=0;
	}

    $totalsuccess = sprintf("%.2f", $totalsuccess);
    $totalfail = sprintf("%.2f", $totalfail);
    $total = sprintf("%.2f", $totalsuccess+$totalfail);

	$return_clicent = get_defined_vars();
return view('/yxpdd/mycomm', $return_clicent);
}elseif($op == 'tixian'){
	$code=0;
	if ($system['autored']==2) {
		$errormsg = '';
		$totalsuccess = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
		            WHERE m.themeid = :themeid and (status=1 or status=98 and type=99) and m.openid = :openid',$pm);

		$hongbaocishu = db_fetchcolumn ( 'SELECT count(id) FROM ' . tablename (t_comm) . ' as m 
		            WHERE m.themeid = :themeid and type=99 and addtime>=' . strtotime(date("Y-m-d")) . ' and m.openid = :openid',$pm);

		if ($config['comm4autored']==3){
			$totalfail = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
			            WHERE m.themeid = :themeid and status=0 and type<>99 and m.openid = :openid',$pm);
		}else{
			$totalfail = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
			            WHERE m.themeid = :themeid and status=0 and type<>4 and type<>7 and type<>99 and m.openid = :openid',$pm);
		}
	    if ($totalfail==0) {
	    	$errormsg = '无可提现金额';
	    }elseif ($totalfail<0.3) {
	    	$errormsg = '最少提现金额为0.3元';
	    }
	    if ($totalfail>500) {
	    	$errormsg = '金额大于500元需联系工作人员提现';
	    }
	    if ($hongbaocishu>5){
	    	$errormsg = '今日已提现'. $hongbaocishu .'次已达上限，明日再来提现' ;
	    }

	    $totalsuccess = sprintf("%.2f", $totalsuccess);
	    $totalfail = sprintf("%.2f", $totalfail);
		if ($totalfail<$config['hbtixian']) {			
			if ($totalsuccess<$config['hbtixian']) {	
				$errormsg = '未达到提现金额';
			}elseif($config['hbtixianmode'] == 1){
				$errormsg = '未达到提现金额';
			}
		}
		if ($errormsg=="") {
		    $result = Comm::addhbtixian($system,$config,$totalfail);
		    if ($result!=false && intval($result)>0) {
		    	$row = Comm::getCommtixian($result);
			    $r = array();
			    if(!empty($row)){
		            $settings = "commid:".$row['id'];
		            $r = Fee::commRecord($row,$system);
		            if ($r[0]==0){
				    	$errormsg="提现成功，30秒内即可到微信零钱";
						$code=1;
		            }
			    }
		    }
		}
	}else{
		$errormsg = '无权操作';
	}
	$result = array();
	$result['errormsg']=$errormsg;
	$result['code']=$code;
	die(json_encode($result));

}else{
	$pageNumber = max(1, intval($_P['pg']));
	$pageSize = 20;		
	// $sql = "SELECT m.type,m.fee,s.fee,m.fromuid,m.uid,m.addtime FROM ".tablename(t_comm)." as m 
	// 			INNER JOIN ".tablename(t_commshare)." as s on m.uid = s.uid
	//             WHERE m.themeid = :themeid and m.status=1 and s.status<2 and m.openid = :openid
	//             order by id desc
	//             LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;


	if (!($config['iscommshowfail'] == 1  || $system['autored']==2)){
		$comm1txt=" and status=1 ";
	}
	if ($config['iscommshowfail'] == 0){
		$comm2txt=" and status=1 ";
	}else{
		$comm2txt=" and status<2 ";
	}
	$sql="SELECT * FROM (
		select type,fee,fromuid,uid,addtime,msg,status,1 as comm
		from ".tablename(t_comm)."
		WHERE  themeid = :themeid and uid = :uid ".$comm1txt."

		union all
		select type,fee,fromuid,uid,addtime,msg,status,2 as comm 
		from ".tablename(t_commshare)."
		WHERE  themeid = :themeid and  uid = :uid and type<>4 and type<>7 ".$comm2txt.") as t

		ORDER BY addtime desc,fee desc,comm
	    LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
	$list = db_select($sql,$pu);
	// yxlog($pu);
	foreach ($list as $index => $row){
		if ($row['type']==4 || $row['type'] ==  7){
			$commfanslist = Fans::getById($row['uid'],$themeid);
		}else{
			$commfanslist = Fans::getById($row['fromuid'],$themeid);
		}
        $list[$index]['time'] = date("Y-m-d H:i:s",$row['addtime']);
        $list[$index]['avatar'] = $commfanslist['avatar'];
        $list[$index]['nickname'] = mb_substr($commfanslist['nickname'],0,9,'utf-8');
        if ($row['type'] ==  0) {
            $list[$index]['leixing'] = '好友<br>下单';
        }else if ($row['type'] ==  1) {
            $list[$index]['leixing'] = '好友<br>下单';
        }else if ($row['type'] ==  2) {
            $list[$index]['leixing'] = '二级<br>分销';
        }else if ($row['type'] ==  3) {
            $list[$index]['leixing'] = '三级<br>分销';
        }else if ($row['type'] ==  4 && $row['comm'] ==  1) {
            $list[$index]['leixing'] = '转发<br>红包';
        }else if ($row['type'] ==  4 && $row['comm'] ==  2) {
            $list[$index]['leixing'] = '转发<br>记录';
        }else if ($row['type'] ==  5) {
            $list[$index]['leixing'] = '阅读<br>红包';
        }else if ($row['type'] ==  6) {
            $list[$index]['leixing'] = '员工<br>提成';
        }else if ($row['type'] ==  7 && $row['comm'] ==  1) {
            $list[$index]['leixing'] = '摇一摇<br>红包';
        }else if ($row['type'] ==  7 && $row['comm'] ==  2) {
            $list[$index]['leixing'] = '摇一摇<br>记录';
        }else if ($row['type'] ==  10) {
            $list[$index]['leixing'] = '进入<br>链接';
        }else if ($row['type'] ==  11) {
            $list[$index]['leixing'] = '自己<br>报名';
        }else if ($row['type'] ==  99 && $row['status']==98) {
            $list[$index]['leixing'] = '等待<br>到帐';
        }else if ($row['type'] ==  99 && $row['status']==99) {
            $list[$index]['leixing'] = '提现<br>到帐';
        }else if ($row['type'] ==  9) {
            $list[$index]['leixing'] = '好友<br>下单';
        }else{
            $list[$index]['leixing'] = '其它';
        }
    }
    $sql="SELECT COUNT(*) FROM (
		select type,fee,fromuid,uid,addtime,msg,status 
		from ".tablename(t_comm)."
		WHERE  themeid = :themeid and  uid = :uid

		union all
		select type,fee,uid,uid,addtime,msg,status 
		from ".tablename(t_commshare)."
		WHERE  themeid = :themeid and  uid = :uid and status<2) as t

		ORDER BY addtime desc,fee desc";

	$total = db_fetchcolumn ($sql ,$pu);

	if ($total==0){
		$result['code'] = 0;
	}elseif ($total<=$pageNumber*$pageSize){
		$result['isend'] = 1;
	}
		$result['data'] = $list;
	die(json_encode($result));
}
}
}